#include "stdafx.h"
//#include "FilterTreeBox.h"

namespace ui
{

	class FilterTreeView : public ui::TreeView
	{
	public:
		FilterTreeView(){

		};
		virtual ~FilterTreeView(){}

		UiSize EstimateFixedSize(const UiSize& szFixed, const UiSize& szMax);
		
	};

	UiSize FilterTreeView::EstimateFixedSize(const UiSize& szFixed, const UiSize& szMax) {
		int nWidth = szFixed.cx;
		int indent = GetIndent();
		UiRect rcThisPadding = GetPadding();
		int nListCount = GetCount();
		if (szFixed.cx == DUI_LENGTH_AUTO) {
			int autoWidth = 0;
			for (int it = 0; it < nListCount; it++) {
				Control* pControl = GetItemAt(it);
				if (!pControl->IsVisible())
					continue;
				FilterTreeNode* pNode = static_cast<FilterTreeNode*>(pControl);
				if (pNode)
				{
					int width = pNode->GetEstimateWidth();
					width += pNode->GetMargin().left + pNode->GetMargin().right;
					int deep = pNode->GetDepth();
					width += deep * indent;
					if (autoWidth < width)
						autoWidth = width;
				}
			}
			nWidth = autoWidth + rcThisPadding.left + rcThisPadding.right;
		}
		if (nWidth > szMax.cx)
			nWidth = szMax.cx;

		UiSize szAvailable = { nWidth , szMax.cy };
		int nChildMargin = GetChildMargin();
		int cyFixed = 0;
		for (int it = 0; it < nListCount; it++) {
			Control* pControl = GetItemAt(it);
			if (!pControl->IsVisible()) continue;
			UiSize sz = pControl->EstimateSize(szAvailable);
			cyFixed += sz.cy;
		}
		cyFixed += nListCount > 0 ? nChildMargin * (nListCount - 1) : 0;
		
		//int offy = rcThisPadding.top + rcThisPadding.bottom;
		cyFixed += rcThisPadding.top + rcThisPadding.bottom;
		if (cyFixed > szMax.cy)
			cyFixed = szMax.cy;
		return { nWidth, cyFixed };
	}

} // namespace ui
